package com.snjgm.request_network_client;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    public static final String TAG = "MainActivity";
    public static final String PACKAGE_NAME = "com.snjgm.request_network_server";
    public static final String SERVER_ACTION = "com.snjgm.request_network_server.intent.action.RequestNetWorkService";
    private static Messenger mServiceMessenger;
    private Messenger mCientMessenger = new Messenger(new MessengerHandler());
    private int responseCode;

    private static ServiceConnection conn = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder service) {
            Log.d(TAG, "onServiceConnected: 恭喜你，建立了连接");
            mServiceMessenger = new Messenger(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_startBindService).setOnClickListener(this);
        findViewById(R.id.btn_request_network).setOnClickListener(this);
        findViewById(R.id.btn_stoptBindService).setOnClickListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //点击开始绑定按钮，进行对service的绑定
            case R.id.btn_startBindService:
                Intent intent = new Intent();
                intent.setPackage(PACKAGE_NAME);
                intent.setAction(SERVER_ACTION);
                boolean b = bindService(intent, conn, BIND_AUTO_CREATE);
                Log.d(TAG, "bind绑定成功了？" + b);
                break;
            //点击请求网络按钮，客户端向服务端发送提示消息，提示服务端该执行请求网络服务了
            case R.id.btn_request_network:
                Message msg = Message.obtain();
                Bundle bundle = new Bundle();
                msg.what = Constants.MSG_FROM_CLIENT;
                msg.replyTo = mCientMessenger;
                msg.setData(bundle);
                try {
                    mServiceMessenger.send(msg);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
            //点击停止服务绑定按钮，和服务端直接断开连接
            case R.id.btn_stoptBindService:
                unbindService(conn);
                break;
        }
    }

    private class MessengerHandler extends Handler {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            Bundle bundle = (Bundle) msg.obj;
            int mServiceCode = msg.what;
            if (mServiceCode == Constants.MSG_FROM_SERVICE) {
                //下面这个是采用setData的方法,我们还可以采用obj的方法
                //responseCode = msg.getData().getInt("response_code");
                //下面采用msg.obj的方式进行取值
                responseCode = bundle.getInt("response_code");
                Log.d(TAG, "StatusCode: " + responseCode);
                if (responseCode == 200) {
                    Log.d(TAG, "恭喜你,访问百度成功了 ");
                }
            }
        }
    }
}